{% extends 'envs_python/layout_envs_python.html' %}
{% load static %}
{% load foundation_tags %}

{% block html_js_code %}
{{ block.super }}
<script>
layui.use(function(){
  var layer = layui.layer;
  var util = layui.util;

  // 事件绑定
  util.on('lay-on', {
    'layer_uninstall': function(obj){
      var loadIndex = layer.msg('卸载中...', {
        icon: 16,
        shade: 0.01
      });

      var url = this.getAttribute('href');

      fetch(url)
        .then(function(response) {
          return response.json();
        })
        .then(function(data) {
          layer.close(loadIndex);
          if (data.status === "success") {
            layer.msg('卸载成功', { icon: 1 });
            location.reload();
          } else {
            layer.msg('卸载失败: ' + data.message, { icon: 2 });
          }
        })
        .catch(function(error) {
          layer.close(loadIndex);
          layer.msg('请求失败，请重试', { icon: 5 });
          console.error('Error:', error);
        });
      return false;
    },
    "open_explorer": function(obj){
        var url = obj.data('url');
        openFolder(url);
        return false;
    },
    "openTerminal": function(obj){
        var url = obj.data('url');
        openTerminal(url);
        return false;
    },
      "copy-full-path-btn": function(obj){
          var fullPath = obj.data('path');
          copyToClipboard(fullPath);
          return false;
      }
  });
});

</script>
{% endblock %}

{% block page_content %}
{{ block.super }}
{% include 'include_messages.html' %}
<div class="card">
    <div class="card-header p-2">
        <div class="card-title">
             <a href="{% url 'envs_python_installer:python_install' %}" class="btn bg-success">
              <i class="fas fa-plus"></i> 安装
            </a>
        </div>
        <div class="card-tools pr-2">
<a href="JavaScript:location.reload();" class="btn btn-outline-info"><i class="fas fa-sync"></i> 刷新列表</a>
<a href="{% url 'envs_python_installer:reset_default_python' %}" class="btn btn-outline-info"><i class="far fa-window-restore"></i> 重置默认</a>
<a href="{% url 'envs_python_installer:config' %}" class="btn btn-outline-info"><i class="fas fa-cog"></i>  配置安装</a>
<a href="{% url 'envs_python_installer:clear_cache' %}" class="btn btn-outline-info"><i class="fas fa-trash"></i> 清理下载缓存</a>
        </div>
    </div>
    <div class="card-body p-2">
<table class="table table-hover table-striped">
    <thead>
    <tr>
        <th>版本</th>
        <th>py</th>
        <th>环境变量</th>
        <th>解释器路径</th>
        <th></th>
    </tr>
    </thead>
    <tbody>
    {% for version, python in pythons.items %}
    <tr>
        <td>{{ python.name }}</td>
        <td>{% if python.is_py_default %}默认{% else %}-{% endif %}</td>
        <td>{% if python.folder == default_python.path %}默认{% else %}-{% endif %}</td>
        <td>
<button class="btn btn-outline-secondary btn-xs"
        data-url="{% url 'sharedkit:open_explorer' %}?path={% replace_str python.folder '\\' '/' %}"
        title="打开文件夹" lay-on="open_explorer">
    <i class="far fa-folder-open"></i>
</button>
<button class="btn btn-outline-secondary btn-xs" data-path="{% replace_str python.folder '/' '\\'  %}python.exe" title="复制完整路径" lay-on="copy-full-path-btn">
    <i class="far fa-copy"></i>
</button>
<button type="button" data-url="{% url 'sharedkit:open_terminal' %}?path={% replace_str python.folder '\\' '/' %}"
        class="btn btn-outline-secondary btn-xs" title="打开终端命令行" lay-on="openTerminal">
    <i class="fa fa-terminal fa-xs"></i>
</button>
            {% replace_str python.folder '/' '\\'  %}python.exe
        </td>
        <td>
{% if python.version_major == 3 and python.version_minor >= 5 %}
 <a href="{% url 'envs_python_installer:set_py_default_python' version %}"
    class="btn btn-outline-info btn-xs{% if python.is_py_default %} disabled{% endif %}">py默认</a>
<a href="{% url 'envs_python_installer:set_env_default_python' version %}"
   class="btn btn-outline-info btn-xs{% if python.folder == default_python.path %} disabled{% endif %}">环境变量默认</a>
<a href="{% url 'envs_python_installer:package_list' version %}" class="btn btn-outline-info btn-xs">包管理</a>
<a href="{% url 'envs_python_installer:python_uninstall' version %}" class="btn btn-outline-info btn-xs">卸载</a>
    {% else %}
    不支持
{% endif %}
        </td>
    </tr>
    {% endfor %}
    </tbody>
</table>
    </div>
</div>

{% endblock %}